<?php

namespace Modules\Asset\Exports;

use Maatwebsite\Excel\Concerns\Exportable;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithTitle;

use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\ShouldAutoSize;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;

// use PHPExcel_Cell_DataType;
use PhpOffice\PhpSpreadsheet\Cell\DataType;

use Modules\Order\Entities\Order;

class RedpackRecordExport implements WithColumnFormatting, FromCollection, WithTitle, WithHeadings, WithMapping, ShouldAutoSize
{
    use Exportable;

    private $list;

    public function __construct($list)
    {
        $this->list = $list;
    }

    public function columnFormats(): array
    {
        return [];
        // return [
        //     'O' => DataType::TYPE_STRING
        // ];
    }

    /**
     * @return Builder
     */
    public function collection()
    {
        return $this->list;
        // return BookInfo::where('booking_session_id', $this->session->id)->displayed();
    }

    /**
     * @return string
     */
    public function title(): string
    {
        return '记录导出';
    }

    public function map($item): array
    {
        $user = $item->user;

        $subTypeMap = [
            1 => '系统变动',
            2 => '后台单个修改',
            3 => '批量导入修改'
        ];

        $res = [
            $item->id,
            $user->name,
            $user->phone,
            $user->status === 1 ? '正常' : '已禁用',
            $item->value / 100,
            $item->description,
            $item->order_number,
            $item->after / 100,
            @$subTypeMap[$item->sub_type],
            @$item->admin->name,
            $item->created_at
        ];

        return $res;
    }

    public function headings(): array
    {
        return [
            '记录ID',
            '用户名',
            '手机号',
            '用户状态',
            '变动金额',
            '变动描述',
            '订单编号',
            '变动后余额',
            '变动类型',
            '操作人',
            '变动时间',
        ];
    }
}
